package com.kfm.base.manager;

import java.io.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

public class BookManager {

    // 数据库连接信息
    private final String url = "jdbc:mysql://localhost:3306/manager";
    private final String user = "root";
    private final String pass = "root";

    // 数据库工具类
    private DBUtil dbUtil;

    static{
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }


    public BookManager(){
        this.dbUtil = new DBUtil(url, user, pass);
        init();
    }

    public void init(){
        boolean booksFlag = checkTable("books");
        boolean logFlag = checkTable("log");
        if (!(booksFlag && logFlag)){
            // 表不存在
            createTables(booksFlag, logFlag);
        }
    }

    private void createTables(boolean booksFlag, boolean logFlag) {
        final String sqlFile = "sql.txt";
        try (InputStream is = new FileInputStream(sqlFile);) {
            byte[] bytes = is.readAllBytes();
            String sql = new String(bytes);

            String[] split = sql.split(";");

            if (!booksFlag){
                System.out.println("创建 books 表");
                dbUtil.execute(split[0].trim());
            }
            if (!logFlag){
                System.out.println("创建 log 表");
                dbUtil.execute(split[1].trim());
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 检测数据库中是否有 {tableName} 表存在
     * @param tableName
     */
    private boolean checkTable(String tableName) {
        System.out.println("检查 " + tableName + " 表是否存在");
        Connection connection = dbUtil.getConnection();
        try {
            ResultSet resultSet = connection.getMetaData().getTables(null, "manager", tableName, new String[]{"TABLE"});
//            ResultSetMetaData metaData = resultSet.getMetaData();
            if (resultSet.next()){
//                for (int i = 1; i <= metaData.getColumnCount(); i++) {
//                    System.out.print(metaData.getColumnLabel(i));
//                    System.out.print(resultSet.getObject(i) + "\t");
//                }
//                System.out.println();
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            dbUtil.close();
        }
        return false;
    }

    private boolean checkTable2(String tableName) {
        String sql = "select count(*) from " + tableName;

        try {
            dbUtil.execute(sql);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(tableName + "不存在");
            return false;
        }
    }

    private boolean checkTable1(String tableName) {
        String sql = "show tables";

        try {
            ResultSet resultSet = dbUtil.executeQuery(sql);
            while (resultSet.next()){
                String table = resultSet.getString(1);
                if (tableName.equals(table)){
                    return true;
                }
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return false;
    }

    public void addBook(){

//        dbUtil.update("insert into books() values()");
//        String sql = "insert into log () values(?)";
//        String content = "添加 xxxx 书";
//        dbUtil.update(sql);
    }
}
